package com.inputabc.qrcoderv1.ui.mainform.event;

import com.inputabc.EzGUIFramework.listener.ListenerBinder;
import com.inputabc.EzGUIFramework.util.MessageBox;
import com.inputabc.EzGUIFramework.util.alias.Ez;
import com.inputabc.qrcoderv1.ui.mainform.component.MainFrame;
import com.inputabc.qrcoderv1.ui.mainform.component.MainPanel;

import com.inputabc.qrcoderv1.ui.mainform.component.QRPanel;
import com.inputabc.qrcoderv1.ui.mainform.component.SettingPanel;
import com.inputabc.qrcoderv1.util.CacheConfig;
import com.inputabc.qrcoderv1.util.DefaultFileFilter;
import com.inputabc.qrcoderv1.util.MyException;
import com.inputabc.qrcoderv1.util.QRCodeUtil;
import org.apache.commons.lang3.StringUtils;

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.filechooser.FileNameExtensionFilter;

import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.text.ParseException;

/**
 * 统一事件处理器
 *
 * @author gaoweiyi
 */
public class EventHandler {
    public void event(MainFrame mainFrame) {
        ListenerBinder listenerBinder = Ez.getListenerBinder(this);

        JMenuItem cutMenu = mainFrame.getMainPanel().getSettingPanel().getCutMenu();
        JMenuItem copyMenu = mainFrame.getMainPanel().getSettingPanel().getCopyMenu();
        JMenuItem pasteMenu = mainFrame.getMainPanel().getSettingPanel().getPasteMenu();
        JTextArea contentTextArea = mainFrame.getMainPanel().getSettingPanel().getContentTextArea();
        listenerBinder.actionPerformed(cutMenu, "cut", contentTextArea);
        listenerBinder.actionPerformed(copyMenu, "copy", contentTextArea);
        listenerBinder.actionPerformed(pasteMenu, "paste", contentTextArea);

        JButton makeButton = mainFrame.getMainPanel().getSettingPanel().getMakeButton();
        MainPanel mainPanel = mainFrame.getMainPanel();
        listenerBinder.mouseEntered(makeButton, "showBorder", mainFrame.getMainPanel().getSettingPanel());
        listenerBinder.mouseExited(makeButton, "hideBorder", mainFrame.getMainPanel().getSettingPanel());
        listenerBinder.actionPerformed(makeButton, "makeQRCode", mainFrame.getMainPanel().getQrPanel().getQrcodeImageLabel(), contentTextArea);

        JButton exportButton = mainFrame.getMainPanel().getQrPanel().getExportButton();
        listenerBinder.mouseEntered(exportButton, "showBorder", mainFrame.getMainPanel().getQrPanel());
        listenerBinder.mouseExited(exportButton, "hideBorder", mainFrame.getMainPanel().getQrPanel());

        JButton importButton = mainFrame.getMainPanel().getQrPanel().getImportButton();
        listenerBinder.mouseEntered(importButton, "showBorder", mainFrame.getMainPanel().getQrPanel());
        listenerBinder.mouseExited(importButton, "hideBorder", mainFrame.getMainPanel().getQrPanel());

        JComboBox<String> charsetComboBox = mainFrame.getMainPanel().getSettingPanel().getCharsetComboBox();
        listenerBinder.itemStateChanged(charsetComboBox, "charsetItemSelected", charsetComboBox);

        JComboBox<String> formatComboBox = mainFrame.getMainPanel().getSettingPanel().getFormatComboBox();
        listenerBinder.itemStateChanged(formatComboBox, "formatItemSelected", formatComboBox);

        JSpinner qrcodeSizeSpinner = mainFrame.getMainPanel().getSettingPanel().getQrcodeSizeSpinner();
        qrcodeSizeSpinner.addChangeListener(ce -> {
            int value = (int) qrcodeSizeSpinner.getValue();
            updateSpinnerEditor(qrcodeSizeSpinner, value);
            SpinnerNumberModel qrcodeSizeSpinnerModel = (SpinnerNumberModel) qrcodeSizeSpinner.getModel();
            SpinnerNumberModel compressedWidthSpinnerModel = (SpinnerNumberModel) mainFrame.getMainPanel().getSettingPanel().getCompressedWidthSpinner().getModel();
            SpinnerNumberModel compressedHeightSpinnerModel = (SpinnerNumberModel) mainFrame.getMainPanel().getSettingPanel().getCompressedHeightSpinner().getModel();
            if (value < (int) qrcodeSizeSpinnerModel.getMinimum()) {
                JSpinner.DefaultEditor editor = (JSpinner.DefaultEditor) qrcodeSizeSpinner.getEditor();
                editor.getTextField().setValue(qrcodeSizeSpinnerModel.getMinimum());
                updateSpinnerEditor(qrcodeSizeSpinner, value);
                qrcodeSizeSpinner.setValue(qrcodeSizeSpinnerModel.getMinimum());
                value = (int) qrcodeSizeSpinnerModel.getMinimum();
            } else if (value > (int) qrcodeSizeSpinnerModel.getMaximum()) {
                JSpinner.DefaultEditor editor = (JSpinner.DefaultEditor) qrcodeSizeSpinner.getEditor();
                editor.getTextField().setValue(qrcodeSizeSpinnerModel.getMaximum());
                updateSpinnerEditor(qrcodeSizeSpinner, value);
                qrcodeSizeSpinner.setValue(qrcodeSizeSpinnerModel.getMaximum());
                value = (int) qrcodeSizeSpinnerModel.getMaximum();
            }
            QRCodeUtil.setQrcodeSize(value);
            int compressedMaxSize = (int) (value * 0.3);
            compressedWidthSpinnerModel.setMaximum(compressedMaxSize);
            compressedHeightSpinnerModel.setMaximum(compressedMaxSize);
        });

        listenerBinder.actionPerformed(mainFrame.getMainPanel().getSettingPanel().getInsertLogoButton(), "insertLogo"
                , mainFrame.getMainPanel().getSettingPanel().getCompressLogoCheckBox()
                , mainFrame.getMainPanel().getSettingPanel().getCancelLogoButton()
                , mainFrame.getMainPanel().getSettingPanel().getCompressedWidthSpinnerLabel()
                , mainFrame.getMainPanel().getSettingPanel().getCompressedWidthSpinner()
                , mainFrame.getMainPanel().getSettingPanel().getCompressedHeightSpinnerLabel()
                , mainFrame.getMainPanel().getSettingPanel().getCompressedHeightSpinner());

        listenerBinder.itemStateChanged(mainFrame.getMainPanel().getSettingPanel().getCompressLogoCheckBox(),
                "toggleEnableCompressLogo"
                , mainFrame.getMainPanel().getSettingPanel().getCompressedWidthSpinnerLabel()
                , mainFrame.getMainPanel().getSettingPanel().getCompressedWidthSpinner()
                , mainFrame.getMainPanel().getSettingPanel().getCompressedHeightSpinnerLabel()
                , mainFrame.getMainPanel().getSettingPanel().getCompressedHeightSpinner());

        JSpinner compressedWidthSpinner = mainFrame.getMainPanel().getSettingPanel().getCompressedWidthSpinner();
        JSpinner compressedHeightSpinner = mainFrame.getMainPanel().getSettingPanel().getCompressedHeightSpinner();
        compressedWidthSpinner.addChangeListener(ce -> {
            int value = (int) compressedWidthSpinner.getValue();
            updateSpinnerEditor(compressedWidthSpinner, value);
            SpinnerNumberModel compressedWidthSpinnerModel = (SpinnerNumberModel) compressedWidthSpinner.getModel();
            if (value > (int) compressedWidthSpinnerModel.getMaximum()) {
                JSpinner.DefaultEditor editor = (JSpinner.DefaultEditor) compressedWidthSpinner.getEditor();
                editor.getTextField().setValue(compressedWidthSpinnerModel.getMaximum());
                updateSpinnerEditor(compressedWidthSpinner, value);
                compressedWidthSpinner.setValue(compressedWidthSpinnerModel.getMaximum());
                QRCodeUtil.setWidth((Integer) compressedWidthSpinnerModel.getMaximum());
                return;
            }
            QRCodeUtil.setWidth(value);
        });

        compressedHeightSpinner.addChangeListener(ce -> {
            int value = (int) compressedHeightSpinner.getValue();
            updateSpinnerEditor(compressedHeightSpinner, value);
            SpinnerNumberModel compressedHeightSpinnerModel = (SpinnerNumberModel) compressedHeightSpinner.getModel();
            if (value > (int) compressedHeightSpinnerModel.getMaximum()) {
                JSpinner.DefaultEditor editor = (JSpinner.DefaultEditor) compressedHeightSpinner.getEditor();
                editor.getTextField().setValue(compressedHeightSpinnerModel.getMaximum());
                updateSpinnerEditor(compressedHeightSpinner, value);
                compressedHeightSpinner.setValue(compressedHeightSpinnerModel.getMaximum());
                QRCodeUtil.setHeight((Integer) compressedHeightSpinnerModel.getMaximum());
                return;
            }
            QRCodeUtil.setHeight(value);
        });

        listenerBinder.actionPerformed(mainFrame.getMainPanel().getSettingPanel().getCancelLogoButton(), "cancelLogo",
                mainFrame.getMainPanel().getSettingPanel().getCompressLogoCheckBox()
                , mainFrame.getMainPanel().getSettingPanel().getCancelLogoButton()
                , mainFrame.getMainPanel().getSettingPanel().getCompressedWidthSpinnerLabel()
                , mainFrame.getMainPanel().getSettingPanel().getCompressedWidthSpinner()
                , mainFrame.getMainPanel().getSettingPanel().getCompressedHeightSpinnerLabel()
                , mainFrame.getMainPanel().getSettingPanel().getCompressedHeightSpinner()
                , mainFrame.getMainPanel().getQrPanel().getQrcodeImageLabel()
                , mainFrame.getMainPanel().getSettingPanel().getContentTextArea());

        listenerBinder.actionPerformed(mainFrame.getMainPanel().getSettingPanel().getResetButton()
                , "reset", mainFrame.getMainPanel());

        listenerBinder.actionPerformed(mainFrame.getMainPanel().getQrPanel().getExportButton()
                , "export"
                , mainFrame);

        listenerBinder.actionPerformed(mainFrame.getMainPanel().getQrPanel().getImportButton()
                , "importByNative", mainFrame);
    }

    private void updateSpinnerEditor(JSpinner spinner, int value) {
        try {
            spinner.commitEdit();
        } catch (ParseException pe) {
            {
                JComponent editor = spinner.getEditor();
                if (editor instanceof JSpinner.DefaultEditor) {
                    ((JSpinner.DefaultEditor) editor).getTextField().setValue(spinner.getValue());
                }
                spinner.setValue(value);
            }
        }

    }

    public void cut(JTextArea sourceTextArea) {
        sourceTextArea.cut();
    }

    public void copy(JTextArea sourceTextArea) {
        sourceTextArea.copy();
    }

    public void paste(JTextArea sourceTextArea) {
        sourceTextArea.paste();
    }

    public void showBorder(MouseEvent me, JPanel parentPanel) {
        JButton source = (JButton) me.getSource();
        source.setBorder(BorderFactory.createLineBorder(Color.lightGray));
        source.setBackground(new Color(242, 242, 242));
        source.updateUI();
    }

    public void hideBorder(MouseEvent me, JPanel parentPanel) {
        JButton source = (JButton) me.getSource();
        source.setBorder(BorderFactory.createEmptyBorder());
        source.setBackground(new Color(242, 242, 242));
        source.updateUI();
    }

    public void charsetItemSelected(JComboBox charsetComboBox) {
        String charset = (String) charsetComboBox.getModel().getSelectedItem();
        QRCodeUtil.setCharset(charset);
    }

    public void formatItemSelected(JComboBox formatComboBox) {
        String format = (String) formatComboBox.getModel().getSelectedItem();
        QRCodeUtil.setFormatName(format);

    }

    public void makeQRCode(JLabel qrcodeLabel, JTextArea contentTextArea) {
        qrcodeLabel.setIcon(null);
        String text = contentTextArea.getText();
        if (StringUtils.isBlank(text)) {
            contentTextArea.requestFocus();
            return;
        }
        try {
            BufferedImage image = QRCodeUtil.createImage(text);
            if (StringUtils.isNotBlank(CacheConfig.logoPath)) {
                QRCodeUtil.insertImage(image, CacheConfig.logoPath, CacheConfig.compressLogo);
            }
            Image image1 = image.getScaledInstance(qrcodeLabel.getWidth() - 3, qrcodeLabel.getHeight() - 3,
                    Image.SCALE_SMOOTH);
            BufferedImage tag = new BufferedImage(qrcodeLabel.getWidth() - 3, qrcodeLabel.getHeight() - 3,
                    BufferedImage.TYPE_INT_RGB);
            Graphics g = tag.getGraphics();
            g.drawImage(image1, 3, 3, null);
            g.dispose();
            qrcodeLabel.setIcon(new ImageIcon(image1));
            qrcodeLabel.updateUI();
            qrcodeLabel.repaint();

            //开启导出按钮
            JButton exportButton = (JButton) Ez.getComponentWithCrossParentByNameEqual(qrcodeLabel, "exportButton");
            exportButton.setEnabled(true);
        } catch (MyException me) {
            me.printStackTrace();
            MessageBox.alert(me.getMessage(), "错误", "error");
            return;
        } catch (Exception e) {
            e.printStackTrace();
            MessageBox.alert(e.getMessage(), "致命错误", "error");
            return;
        }

    }

    public void insertLogo(JCheckBox compressLogoCheckBox, JButton cancelLogoButton
            , JLabel compressedWidthSpinnerLabel, JSpinner compressedWidthSpinner
            , JLabel compressedHeightSpinnerLabel, JSpinner compressedHeightSpinner) {
        try {
            JFileChooser fileChooser = new JFileChooser();
            Ez.removeButtonDottedLine(fileChooser);
            fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
            fileChooser.setFileHidingEnabled(true);
            fileChooser.setDialogTitle("选择一个Logo文件");
            fileChooser.setDialogType(JFileChooser.OPEN_DIALOG);
            DefaultFileFilter filter = new DefaultFileFilter();
            filter.addExtension("jpg");
            filter.addExtension("png");
            filter.addExtension("gif");
            filter.addExtension("jpeg");
            filter.setDescription("图片文件");
            fileChooser.setFileFilter(filter);
            if (CacheConfig.lastSelectedDirForLogo != null) {
                fileChooser.setCurrentDirectory(CacheConfig.lastSelectedDirForLogo);
            }
            fileChooser.showOpenDialog(CacheConfig.mainFrame);
            File selectedFile = fileChooser.getSelectedFile();
            if (selectedFile == null) {
                return;
            }
            CacheConfig.lastSelectedDirForLogo = selectedFile.getParentFile();
            BufferedImage logoImage = ImageIO.read(selectedFile);
            int width = logoImage.getWidth();
            int height = logoImage.getHeight();
            int qrCodeSize = QRCodeUtil.getQRCodeSize();
            compressLogoCheckBox.setEnabled(true);
            cancelLogoButton.setEnabled(true);
            CacheConfig.logoPath = selectedFile.getAbsolutePath();

            int compressedMaxSize = (int) (qrCodeSize * 0.3);
            SpinnerNumberModel compressedWidthSpinnerModel = (SpinnerNumberModel) compressedWidthSpinner.getModel();
            compressedWidthSpinnerModel.setMaximum(compressedMaxSize);
            compressedWidthSpinnerModel.setValue(compressedMaxSize);
            QRCodeUtil.setWidth(compressedMaxSize);
            SpinnerNumberModel compressedHeightSpinnerModel = (SpinnerNumberModel) compressedHeightSpinner.getModel();
            compressedHeightSpinnerModel.setMaximum(compressedMaxSize);
            compressedHeightSpinner.setValue(compressedMaxSize);
            QRCodeUtil.setHeight(compressedMaxSize);
            if ((width * height) > (compressedMaxSize * compressedMaxSize)) {
                compressLogoCheckBox.setSelected(true);
            }

        } catch (Exception e) {
            e.printStackTrace();
            MessageBox.alert(e.getMessage(), "错误", "error");
            return;
        }

    }

    public void toggleEnableCompressLogo(ItemEvent ie, JLabel compressedWidthSpinnerLabel, JSpinner compressedWidthSpinner
            , JLabel compressedHeightSpinnerLabel, JSpinner compressedHeightSpinner) {
        JCheckBox checkBox = (JCheckBox) ie.getSource();
        if (checkBox.isSelected()) {
            CacheConfig.compressLogo = true;
            compressedWidthSpinnerLabel.setEnabled(true);
            compressedWidthSpinner.setEnabled(true);
            compressedHeightSpinnerLabel.setEnabled(true);
            compressedHeightSpinner.setEnabled(true);
        } else {
            CacheConfig.compressLogo = false;
            compressedWidthSpinnerLabel.setEnabled(false);
            compressedWidthSpinner.setEnabled(false);
            compressedHeightSpinnerLabel.setEnabled(false);
            compressedHeightSpinner.setEnabled(false);
        }
    }

    public void cancelLogo(JCheckBox compressLogoCheckBox, JButton cancelLogoButton
            , JLabel compressedWidthSpinnerLabel, JSpinner compressedWidthSpinner
            , JLabel compressedHeightSpinnerLabel, JSpinner compressedHeightSpinner
            , JLabel qrcodeLabel, JTextArea contentTextArea) {
        CacheConfig.compressLogo = false;
        CacheConfig.logoPath = null;
        compressLogoCheckBox.setSelected(false);
        compressLogoCheckBox.setEnabled(false);
        cancelLogoButton.setEnabled(false);
        compressedWidthSpinnerLabel.setEnabled(false);
        SpinnerNumberModel compressedWidthSpinnerModel = (SpinnerNumberModel) compressedWidthSpinner.getModel();
        compressedWidthSpinnerModel.setValue(0);
        compressedWidthSpinner.setEnabled(false);
        SpinnerNumberModel compressedHeightSpinnerModel = (SpinnerNumberModel) compressedHeightSpinner.getModel();
        compressedHeightSpinnerModel.setValue(0);
        compressedWidthSpinner.setEnabled(false);
        makeQRCode(qrcodeLabel, contentTextArea);
    }

    public void reset(MainPanel mainPanel) {
        SettingPanel settingPanel = mainPanel.getSettingPanel();
        QRPanel qrPanel = mainPanel.getQrPanel();
        JSpinner compressedHeightSpinner = settingPanel.getCompressedHeightSpinner();
        JSpinner compressedWidthSpinner = settingPanel.getCompressedWidthSpinner();
        JLabel compressedHeightSpinnerLabel = settingPanel.getCompressedHeightSpinnerLabel();
        JLabel compressedWidthSpinnerLabel = settingPanel.getCompressedWidthSpinnerLabel();
        JButton makeButton = settingPanel.getMakeButton();
        JSpinner qrcodeSizeSpinner = settingPanel.getQrcodeSizeSpinner();
        JComboBox<String> formatComboBox = settingPanel.getFormatComboBox();
        JComboBox<String> charsetComboBox = settingPanel.getCharsetComboBox();
        JCheckBox compressLogoCheckBox = settingPanel.getCompressLogoCheckBox();
        JTextArea contentTextArea = settingPanel.getContentTextArea();
        JButton cancelLogoButton = settingPanel.getCancelLogoButton();
        JButton insertLogoButton = settingPanel.getInsertLogoButton();
        JButton exportButton = qrPanel.getExportButton();
        JLabel qrcodeImageLabel = qrPanel.getQrcodeImageLabel();

        cancelLogo(compressLogoCheckBox, cancelLogoButton, compressedWidthSpinnerLabel
                , compressedWidthSpinner, compressedHeightSpinnerLabel, compressedHeightSpinner
                , qrcodeImageLabel, contentTextArea);
        charsetComboBox.getModel().setSelectedItem(CacheConfig.charsets[0]);
        formatComboBox.getModel().setSelectedItem(CacheConfig.formats[1]);
        qrcodeSizeSpinner.getModel().setValue(CacheConfig.defaultQRCodeSize);
        qrcodeImageLabel.setIcon(null);
        qrcodeImageLabel.updateUI();
        qrcodeImageLabel.repaint();
        exportButton.setEnabled(false);
        contentTextArea.setText("");
        mainPanel.requestFocus();
    }

    public void export(MainFrame mainFrame) {
        JFileChooser fileChooser = new JFileChooser();
        Ez.removeButtonDottedLine(fileChooser);
        fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        fileChooser.setDialogTitle("选择导出二维码的目录");
        if (CacheConfig.lastSelectedDirForExport != null) {
            fileChooser.setCurrentDirectory(CacheConfig.lastSelectedDirForExport);
        }
        int opt = fileChooser.showSaveDialog(mainFrame);
        if (opt != JFileChooser.APPROVE_OPTION) {
            return;
        }
        File selected = fileChooser.getSelectedFile();
        if (selected.exists() == false) {
            MessageBox.alert("只能选择目录，不能指定文件名！", "警告", "!");
            return;
        }
        CacheConfig.lastSelectedDirForExport = selected;
        try {
            QRCodeUtil.encode(mainFrame.getMainPanel().getSettingPanel().getContentTextArea().getText()
                    , CacheConfig.logoPath
                    , selected.getAbsolutePath()
                    , CacheConfig.compressLogo
                    , null);
        } catch (Exception e) {
            e.printStackTrace();
            MessageBox.alert(e.getMessage(), "错误", "error");
        }
    }

    public void importByNative(MainFrame mainFrame) {
        JFileChooser fileChooser = new JFileChooser();
        Ez.removeButtonDottedLine(fileChooser);
        fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        fileChooser.setDialogTitle("选择一个二维码");
        DefaultFileFilter filter = new DefaultFileFilter();
        filter.addExtension("jpg");
        filter.addExtension("png");
        filter.addExtension("gif");
        filter.addExtension("jpeg");
        filter.setDescription("图片文件");
        fileChooser.setFileFilter(filter);
        if (CacheConfig.lastSelectedDirForImport != null) {
            fileChooser.setCurrentDirectory(CacheConfig.lastSelectedDirForImport);
        }
        fileChooser.showOpenDialog(mainFrame);
        File selectedFile = fileChooser.getSelectedFile();
        if (selectedFile == null) {
            return;
        }
        CacheConfig.lastSelectedDirForImport = selectedFile.getParentFile();
        JLabel qrcodeLabel = null;
        SpinnerNumberModel qrcodeSizeSpinnerModel = null;
        try {
            qrcodeLabel = mainFrame.getMainPanel().getQrPanel().getQrcodeImageLabel();
            BufferedImage image = ImageIO.read(selectedFile);
            Image image1 = image.getScaledInstance(qrcodeLabel.getWidth() - 3, qrcodeLabel.getHeight() - 3,
                    Image.SCALE_SMOOTH);
            BufferedImage tag = new BufferedImage(qrcodeLabel.getWidth() - 3, qrcodeLabel.getHeight() - 3,
                    BufferedImage.TYPE_INT_RGB);
            Graphics g = tag.getGraphics();
            g.drawImage(image1, 3, 3, null);
            g.dispose();
            qrcodeLabel.setIcon(new ImageIcon(image1));
            qrcodeLabel.updateUI();
            qrcodeLabel.repaint();
            int width = image.getWidth();
            int height = image.getHeight();
            qrcodeSizeSpinnerModel = (SpinnerNumberModel) mainFrame.getMainPanel().getSettingPanel().getQrcodeSizeSpinner().getModel();
            if ((width * height) > ((int) qrcodeSizeSpinnerModel.getMaximum() * (int) qrcodeSizeSpinnerModel.getMaximum())) {
                qrcodeSizeSpinnerModel.setValue(qrcodeSizeSpinnerModel.getMaximum());
                QRCodeUtil.setQrcodeSize((Integer) qrcodeSizeSpinnerModel.getMaximum());
            } else if ((width * height) < ((int) qrcodeSizeSpinnerModel.getMinimum() * (int) qrcodeSizeSpinnerModel.getMinimum())) {
                qrcodeSizeSpinnerModel.setValue(qrcodeSizeSpinnerModel.getMinimum());
                QRCodeUtil.setQrcodeSize((Integer) qrcodeSizeSpinnerModel.getMinimum());
            }
            String decoded = QRCodeUtil.decode(selectedFile);
            mainFrame.getMainPanel().getSettingPanel().getContentTextArea().setText(decoded);
            mainFrame.getMainPanel().getQrPanel().getExportButton().setEnabled(true);


        } catch (Exception e) {
            QRCodeUtil.setQrcodeSize(CacheConfig.defaultQRCodeSize);
            qrcodeLabel.setIcon(null);
            qrcodeSizeSpinnerModel.setValue(CacheConfig.defaultQRCodeSize);
            e.printStackTrace();
            MessageBox.alert(e.getMessage(), "错误", "error");
            return;
        }

    }
}
